<template>
  <div class="app-container">
    <eHeader :param-bean="paramBean" :sup_this="sup_this" />
    <eForm ref="form" :sup_this="sup_this" />
    <!--表格渲染-->
    <DataGridTable
      v-loading="loading"
      ref="mainTable"
      :sup_this="sup_this"
      :data="data.rtnList"
      :total="total"
      @selection-change="handleGridSelectionChange">
      <DataGridColumnSlot prop="userAccount" label="用户账号" width="150px" >
        <template slot-scope="scope">
          <edit :data="scope.row" :sup_this="sup_this" />
        </template>
      </DataGridColumnSlot>
      <el-table-column prop="userName" label="姓名" width="150px" />
      <el-table-column prop="phone" label="电话" width="100px" />
      <el-table-column prop="email" label="邮件" width="100px" />
      <el-table-column prop="cpyName" label="所属公司" width="200px" />
      <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.user_status" :key="item.id">
            <el-tag v-if="scope.row.userStatus.toString() === item.dictCode" :type="scope.row.userStatus === 'UNLOCK' ? '' : 'info'">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="有效" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.enable_flag" :key="item.id">
            <el-tag v-if="scope.row.enableFlag.toString() === item.dictCode" :type="scope.row.enableFlag === 'Y' ? '' : 'info'">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建日期" width="200px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['SYS_ADMIN'])" label="操作" width="130px" align="center">
        <template slot-scope="scope">
          <el-button :loading="saveAdminLoading" class="filter-item" size="mini" type="success" icon="el-icon-edit" @click="joinAdminUser(scope.row)">加入管理员</el-button>
        </template>
      </el-table-column>
    </DataGridTable>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { parseTime } from '@/utils/index'
import initData from '@/mixins/initData'
import eHeader from './module/header'
import eForm from './module/form'
import edit from './module/edit'
import { loadDataUrl } from '@/api/user'
import { joinAdminUser } from '@/api/system/role'

export default {
  components: { eHeader, edit, eForm },
  mixins: [initData],
  // 设置数据字典
  dictTypes: ['user_status', 'enable_flag'],
  data() {
    return {
      saveAdminLoading: false,
      sup_this: this
    }
  },
  created() {
    this.url = loadDataUrl()
    this.searchToggle = true
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    async beforeInit() {
      return true
    },
    joinAdminUser(row) {
      this.saveAdminLoading = true
      const data = { busMainData: { sysOwnerCpy: row.cpyCode, roleUserVOList: [] }}
      const tmpRow = { userId: row.id, userAccount: row.userAccount }
      data.busMainData.roleUserVOList.push(tmpRow)

      joinAdminUser(data).then(res => {
        this.saveAdminLoading = false
        this.$notify({
          title: '加入成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.saveAdminLoading = false
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
